using System;
using System.Data;
using System.Collections.Generic;

namespace ADEVSO.Data
{
    public abstract class DataTable : IDataTable<DataTable>
    {
        #region Variables

        #endregion

        #region Constructors

        /// <summary>
        /// Create New Database Table
        /// </summary>
        public DataTable()
        {
        }

        /// <summary>
        /// Create New Database Table
        /// </summary>
        /// <param name="connectionString"></param>
        public DataTable(String connectionString)
        {
            if (Connection != null)
                Connection.InnerConnection.ConnectionString = connectionString;
        }

        /// <summary>
        /// Create New Database Table
        /// </summary>
        /// <param name="connection"></param>
        public DataTable(Connection connection)
        {
            if (connection != null)
                Connection = connection;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Database Connection
        /// </summary>
        protected Connection Connection { get; set; }

        /// <summary>
        /// Database Connection State
        /// </summary>
        public ConnectionState State
        {
            get
            {
                return Connection.InnerConnection.State;
            }
        }

        #endregion

        #region Abstract

        /// <summary>
        /// Get DataRow Info
        /// </summary>
        /// <param name="row"></param>
        protected abstract void GetRowInfo(DataRow row);

        /// <summary>
        /// All fields into an object array
        /// </summary>
        /// <returns></returns>
        protected abstract Object[] Fields { get; }

        /// <summary>
        /// Get Table Info
        /// </summary>
        /// <returns></returns>
        //public abstract List<T> Load();

        #endregion

        #region IDataTable

        /// <summary>
        /// Read data table row by stored procedure
        /// </summary>
        /// <param name="storedProcedure"></param>
        /// <param name="args"></param>
        /// <returns>Return true if data was found. Otherwise false.</returns>
        public bool Row(String storedProcedure, params Object[] args)
        {
            bool result = false;
            DataRowCollection rows = Connection.GetDataTable(storedProcedure, args).Rows;
            if (rows.Count > 0)
            {
                result = true;
                GetRowInfo(rows[0]);
            }
            return result;
        }

        /// <summary>
        /// Return a List of data table rows read by stored procedure
        /// </summary>
        /// <param name="storedProcedure"></param>
        /// <returns></returns>
        public List<T> Rows<T>(String storedProcedure)
        {
            return Rows<T>(storedProcedure, null);
        }

        /// <summary>
        /// Return a List of data table rows read by stored procedure
        /// </summary>
        /// <param name="storedProcedure"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public List<T> Rows<T>(String storedProcedure, params Object[] args)
        {
            System.Data.DataTable table = null;
            if (args == null)
                table = Connection.GetDataTable(storedProcedure);
            else
                table = Connection.GetDataTable(storedProcedure, args);
            List<T> rows = new List<T>();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                GetRowInfo(table.Rows[i]);
                rows.Add((T)MemberwiseClone());
            }
            return rows;
        }

        #endregion

        #region Methods

        #endregion
    }
}
